package org.jz.gae.redbook.spb.server.controller;

import java.util.List;

import org.jz.gae.redbook.spb.server.helper.DistrictHelper;
import org.jz.gae.redbook.spb.server.helper.RedBookHelper;
import org.jz.gae.redbook.spb.shared.DistrictEntityView;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class DistrictControllerGoogle implements DistrictController
{
	@Override
	public void loadDistrictList(
			List<DistrictEntityView> _DistrictList)
	{
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
	    // Run an ancestor query to ensure we see the most up-to-date
	    // view of the Greetings belonging to the selected Guestbook.
	    Query query = new Query(DistrictHelper.ENTITY_NAME, redbookKey).addSort(DistrictHelper.DISTRICT_RUS_NAME, Query.SortDirection.ASCENDING);
	    List<Entity> districts = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    if (districts.isEmpty()) 
	    {
	    	_DistrictList.clear();
	    } 
	    else 
	    {
	    	_DistrictList.clear();
	    	for (Entity district : districts)
	    	{
	    		DistrictEntityView newDistrict = new DistrictEntityView();
	    		DistrictUtils.reflectEntityToView(district, newDistrict);
	    		_DistrictList.add(newDistrict);
	    	}
	    }
	}

	@Override
	public DistrictEntityView loadDistrict(
			String _DistrictId) throws Exception 
	{
		System.err.println("loading key : " + _DistrictId);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity district = datastore.get(KeyFactory.stringToKey(_DistrictId));
		System.err.println("loaded");
		DistrictEntityView result = new DistrictEntityView();
		DistrictUtils.reflectEntityToView(district, result);
		return result;
	}
	
	@Override
	public void updateDistrict(
			DistrictEntityView _DistrictToEdit)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteDistrict(
			DistrictEntityView _DistrictToDelete)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createDistrict(
			DistrictEntityView _DistrictToAppend)
	{
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
		Entity newDistrict = new Entity(DistrictHelper.ENTITY_NAME, redbookKey);
		DistrictUtils.reflectViewToEntity(_DistrictToAppend, newDistrict);
        /**/
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(newDistrict);
		System.err.println("new key : " + newDistrict.getKey());
		_DistrictToAppend.setDistrictKey(KeyFactory.keyToString(newDistrict.getKey()));
	}

}
